﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LicenserProduction
{
    public partial class AddALicense : UserControl
    {
        public AddALicense(ref LicenserInformation ds, int customerId)
        {
            InitializeComponent();
            licenserInformation = ds;
            licensesBindingSource.DataSource = licenserInformation;
            licensesBindingSource.DataMember = "Licenses";

            licenseTypeComboBox.DataSource = licenserInformation.LicenseTypeEnum;
            licenseTypeComboBox.DisplayMember = "Description";
            licenseTypeComboBox.ValueMember = "Code";
            licenseTypeComboBox.SelectedValueChanged += new EventHandler(licenseTypeComboBox_SelectedValueChanged);

            m_customerId = customerId;

            DataRowView drv = (DataRowView)licensesBindingSource.AddNew();
            drv.Row["customerId"] = customerId;
            
        }

        void licenseTypeComboBox_SelectedValueChanged(object sender, EventArgs e)
        {
            if (licenseTypeComboBox.SelectedValue != null)
            {
                DataRowView drv = (DataRowView)licensesBindingSource.Current;
                ((LicenserInformation.LicensesRow)drv.Row).LicenseType = (int)licenseTypeComboBox.SelectedValue;
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            licensesBindingSource.EndEdit();
            if (Synchronize != null)
            {
                Synchronize(this, new EventArgs());
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            licensesBindingSource.CancelEdit();
            if (Synchronize != null)
            {
                Synchronize(this, new EventArgs());
            }
        }

        int m_customerId;
        public delegate void _Synchronize(object sender, EventArgs e);
        public event _Synchronize Synchronize;
    }
}
